package com.stylefeng.guns.modular.poemspoetry.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.stylefeng.guns.core.base.controller.BaseController;
import com.stylefeng.guns.core.common.constant.factory.PageFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.beans.factory.annotation.Autowired;
import com.stylefeng.guns.core.log.LogObjectHolder;
import org.springframework.web.bind.annotation.RequestParam;
import com.stylefeng.guns.modular.poemspoetry.model.Poetry;
import com.stylefeng.guns.modular.poemspoetry.service.IPoetryService;

/**
 * 宋词控制器
 *
 * @author fengshuonan
 * @Date 2018-03-20 15:57:09
 */
@Controller
@RequestMapping("/poetry")
public class PoetryController extends BaseController {

    private String PREFIX = "/poemspoetry/poetry/";

    @Autowired
    private IPoetryService poetryService;

    /**
     * 跳转到宋词首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "poetry.html";
    }

    /**
     * 跳转到添加宋词
     */
    @RequestMapping("/poetry_add")
    public String poetryAdd() {
        return PREFIX + "poetry_add.html";
    }

    /**
     * 跳转到修改宋词
     */
    @RequestMapping("/poetry_update/{poetryId}")
    public String poetryUpdate(@PathVariable Long poetryId, Model model) {
        Poetry poetry = poetryService.selectById(poetryId);
        model.addAttribute("item",poetry);
        LogObjectHolder.me().set(poetry);
        return PREFIX + "poetry_edit.html";
    }

    /**
     * 获取宋词列表
     */
    /*
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(String condition) {
        return poetryService.selectList(null);
    }
    */

     /**
     * 获取宋词列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(String condition) {
        Page<Poetry> page = new PageFactory<Poetry>().defaultPage();
        EntityWrapper<Poetry> ew = new EntityWrapper<Poetry>();
         /*if (ToolUtil.isNotEmpty(condition)) {
                ew.eq("condition", condition);
         }*/
        return super.packForBT(poetryService.selectPage(page,ew));
    }

    /**
     * 新增宋词
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(Poetry poetry) {
        poetryService.insert(poetry);
        return super.SUCCESS_TIP;
    }

    /**
     * 删除宋词
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Long poetryId) {
        poetryService.deleteById(poetryId);
        return SUCCESS_TIP;
    }

    /**
     * 修改宋词
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(Poetry poetry) {
        poetryService.updateById(poetry);
        return super.SUCCESS_TIP;
    }

    /**
     * 宋词详情
     */
    @RequestMapping(value = "/detail/{poetryId}")
    @ResponseBody
    public Object detail(@PathVariable("poetryId") Long poetryId) {
        return poetryService.selectById(poetryId);
    }
}
